package com.xydtech.rule.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.rule.domain.RuleElementsItems;
import com.xydtech.rule.service.IRuleElementsItemsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 额度测算规则Controller
 *
 * @author xydtech
 * @date 2024-06-06
 */
@Controller
@RequestMapping("/rule/items")
public class RuleElementsItemsController extends BaseController {
    private String prefix = "rule/items";


    @Autowired
    private IRuleElementsItemsService ruleElementsItemsService;

    @RequiresPermissions("rule:items:view")
    @GetMapping()
    public String items() {
        return prefix + "/list";
    }

    /**
     * 查询额度测算规则要素清单列表
     */
    @RequiresPermissions("rule:items:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RuleElementsItems ruleElementsItems) {
        startPage();
        List<RuleElementsItems> list = ruleElementsItemsService.selectRuleElementsItemsList(ruleElementsItems);
        return getDataTable(list);
    }

    /**
     * 导出额度测算规则要素清单列表
     */
    @RequiresPermissions("rule:items:export")
    @Log(title = "额度测算规则要素清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RuleElementsItems ruleElementsItems) {
        List<RuleElementsItems> list = ruleElementsItemsService.selectRuleElementsItemsList(ruleElementsItems);
        ExcelUtil<RuleElementsItems> util = new ExcelUtil<RuleElementsItems>(RuleElementsItems.class);
        return util.exportExcel(list, "规则要素清单数据");
    }

    /**
     * 新增额度测算规则要素清单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存额度测算规则要素清单
     */
    @RequiresPermissions("rule:items:add")
    @Log(title = "额度测算规则要素清单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RuleElementsItems ruleElementsItems) {
        ruleElementsItems.setCreateBy(getLoginName());
        return toAjax(ruleElementsItemsService.insertRuleElementsItems(ruleElementsItems));
    }

    /**
     * 修改额度测算规则要素清单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        RuleElementsItems ruleElementsItems = ruleElementsItemsService.selectRuleElementsItemsById(id);
        mmap.put("ruleElementsItems", ruleElementsItems);
        return prefix + "/edit";
    }

    /**
     * 修改保存额度测算规则要素清单
     */
    @RequiresPermissions("rule:items:edit")
    @Log(title = "额度测算规则要素清单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RuleElementsItems ruleElementsItems) {
        return toAjax(ruleElementsItemsService.updateRuleElementsItems(ruleElementsItems));
    }

    /**
     * 删除额度测算规则要素清单
     */
    @RequiresPermissions("rule:items:remove")
    @Log(title = "额度测算规则要素清单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ruleElementsItemsService.deleteRuleElementsItemsByIds(ids));
    }



    /**
     * 查询额度测算规则要素清单列表
     */
    @PostMapping("/getItemsList")
    @ResponseBody
    public TableDataInfo getItemsList(RuleElementsItems ruleElementsItems) {
        startPage();
        List<RuleElementsItems> list = ruleElementsItemsService.getRuleElementsItemsList(ruleElementsItems);
        return getDataTable(list);
    }
}